package databaseproject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
/**
 *
 * @author jesseday
 */
public class User {
    private static Connection con;
    private static String username;
    private static boolean userIsAdmin;
    
    public static boolean doMenu(Connection nCon, String nUsername, boolean nUserIsAdmin)
    {
        con = nCon;
        username = nUsername;
        userIsAdmin = nUserIsAdmin;
        
        boolean alldone = false;

        while (!alldone)
        {
            Menu menu = new Menu();
            menu.setTitle("Users");
            menu.addOption("List Users");
            if (userIsAdmin)
            {
                menu.addOption("Remove a User");
                
            }
            else
            {
                menu.addOption("Delete Account");
            }
            switch (menu.display())
            {
                case(1):
                    boolean done = false;
                    
                    while(!done)
                    {
                        Menu listUsers = new Menu();
                        listUsers.setTitle("Select a User to Display Info:");
                        listUsers.addOptionsFromSQLQuery(con,
                                "select username from APP.\"USER\"");
                        int choice = listUsers.display();
                        
                        if(choice < listUsers.getBackOption())
                        {
                            displayUserInfo(listUsers.getOptionAt(choice));
                        }
                        else
                        {
                            done = true;
                        }
                    }
                    break;
                
                case(2):
                    if (userIsAdmin)
                    {
                        boolean doneDelete = false;
                        
                        while(!doneDelete)
                        {
                            Menu listUsers = new Menu();
                            listUsers.setTitle("\nSelect a User to Remove:");
                            listUsers.addOptionsFromSQLQuery(con,
                                    "select username from APP.\"USER\"");
                            int choice = listUsers.display();

                            if(choice < listUsers.getBackOption())
                            {
                                deleteUser(listUsers.getOptionAt(choice));
                            }
                            else
                            {
                                doneDelete = true;
                            }
                        }
                    }
                    else
                    {
                        deleteUser(username);
                        return false;
                    }
                    break;
                    
                default:
                    alldone = true;
                    break;
            
            }
        }
        return true;
    }
    public static void displayUserInfo(String user)
    {
        try
        {
            PreparedStatement stmt = con.prepareStatement("select * from \"USER\" where"
                    + " username=?",
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
            stmt.setString(1, user);
            ResultSet rs = stmt.executeQuery();
            
            rs.first();
            System.out.println("\nUsername - "+user+"\nName - "+rs.getString("NAME")
                    +"\nBirthdate - "+rs.getDate("BIRTHDATE"));
            
            ResultSet rs2 = stmt.executeQuery("select * from \"USER\" natural "
                    + "join likes where username='" + user + "'");
            
            if(rs2.first())
            {
                System.out.println("\nBands This User Likes:");
                do{
                    System.out.println(rs2.getString("BNAME"));
                            
                }while(rs2.next());
            }
            else
            {
                System.out.println("\nThis User Likes No Bands.");
            }
            
            

        }
        catch (java.sql.SQLException e)
        {
            return;
        }
    }
    public static void deleteUser(String user)
    {
        try
        {
            PreparedStatement stmt = con.prepareStatement("delete from fantasy_song where"
                    + " username=?",
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
            stmt.setString(1, user);
            stmt.executeUpdate();
            
            stmt = con.prepareStatement("delete from fantasy_com where "
                    + " username=?",
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
            stmt.setString(1, user);
            stmt.executeUpdate();
            
            stmt = con.prepareStatement("delete from fantasy_show where"
                    + " username=?",
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
            stmt.setString(1, user);
            stmt.executeUpdate();
            
            stmt = con.prepareStatement("delete from fantasy_com where"
                    + " commenter=?",
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
            stmt.setString(1, user);
            stmt.executeUpdate();
            
            stmt = con.prepareStatement("delete from performance_com where"
                    + " commenter=?",
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
            stmt.setString(1, user);
            stmt.executeUpdate();
            
            stmt = con.prepareStatement("delete from likes where"
                    + " username=?",
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
            stmt.setString(1, user);
            stmt.executeUpdate();
            
            stmt = con.prepareStatement("delete from attends where"
                    + " username=?",
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
            stmt.setString(1, user);
            stmt.executeUpdate();
            
            stmt = con.prepareStatement("delete from \"USER\" where"
                    + " username=?",
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
            stmt.setString(1, user);
            stmt.executeUpdate();
            
        }
        catch (java.sql.SQLException e)
        {
            System.out.print(e);
            return;
        }
    }
    
}
